﻿namespace SmartSoft.DAL
{
    using System;
    using System.Data;
    using NLog;

    public class TaxCodeDataAccess
    {
        private static Logger log = LogManager.GetCurrentClassLogger();
        public void TaxCode_Save(DataTable po_Table, DateTime pd_UdtDate)
        {
            SSDataAccess.RunSQLCommand("DROP PROCEDURE TaxCodeUpdate");
            SSDataAccess.RunSQLCommand(this.TaxCodeString(po_Table, pd_UdtDate));
            SSDataAccess.RunSQLCommand("EXECUTE TaxCodeUpdate");
        }

        public string TaxCodeString(DataTable po_Table, DateTime pd_UdtDate)
        {
            string str = "";
            str = "CREATE PROCEDURE TaxCodeUpdate WITH ENCRYPTION";
            str = (str + "\nAS" + "\nBEGIN") + "\nSET DATEFORMAT dmy" + "\nCREATE TABLE #InvalidTaxList (Ms_Thue NVARCHAR(20), Ten_Dvi NVARCHAR(128), Dia_Chi NVARCHAR(128), Ngay_Bd DATETIME, Dai_Dien NVARCHAR(128), So_CMND NVARCHAR(20), Ngay_Kt DATETIME, UdtDate DATETIME)";
            foreach (DataRow row in po_Table.Rows)
            {
                str = str + "\nINSERT INTO #InvalidTaxList (Ms_Thue, Ten_Dvi, Dia_Chi, Ngay_Bd, Dai_Dien, So_CMND, Ngay_Kt, UdtDate) VALUES ('";
                str = str + row["Ms_Thue"].ToString() + "', " + ((row["Ten_Dvi"] != DBNull.Value) ? ("N'" + row["Ten_Dvi"].ToString() + "'") : "NULL") + ", ";
                str = str + ((row["Dia_Chi"] != DBNull.Value) ? ("N'" + row["Dia_Chi"].ToString() + "'") : "NULL") + ", " + ((row["Ngay_Bd"] != DBNull.Value) ? ("'" + ((DateTime) row["Ngay_Bd"]).ToShortDateString() + "'") : "NULL") + ", ";
                str = str + ((row["Dai_Dien"] != DBNull.Value) ? ("N'" + row["Dai_Dien"].ToString() + "'") : "NULL") + ", " + ((row["So_CMND"] != DBNull.Value) ? ("'" + row["So_CMND"].ToString() + "'") : "NULL") + ", '";
                string[] strArray4 = new string[] { str, ((DateTime) row["Ngay_Kt"]).ToShortDateString(), "', '", pd_UdtDate.ToShortDateString(), "')" };
                str = string.Concat(strArray4);
            }
            return ((str + "\nINSERT INTO InvalidTaxList (Ms_Thue, Ten_Dvi, Dia_Chi, Ngay_Bd, Dai_Dien, So_CMND, Ngay_Kt, UdtDate) SELECT Ms_Thue, Ten_Dvi, Dia_Chi, Ngay_Bd, Dai_Dien, So_CMND, Ngay_Kt, UdtDate FROM #InvalidTaxList ") + "WHERE Ms_Thue NOT IN (SELECT Ms_Thue FROM InvalidTaxList)" + "\nEND");
        }
    }
}

